package de.nonspectare.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;

public class IOHelper {

	public static void createFolder(String path)
			throws CouldNotCreateFolderException {

		if (new File(path).mkdir() == false) {
			throw new CouldNotCreateFolderException(
					"Folder could not be created!");
		}
	}

	public static void createFolders(String foldersPath)
			throws CouldNotCreateFolderException {

		if (new File(foldersPath).mkdirs() == false) {
			throw new CouldNotCreateFolderException(
					"Folders could not be created!");
		}
	}

	public static void callCommandLineTool(String[] args) throws IOException,
			InterruptedException {
		
		System.out.println(Arrays.toString(args));

		try {
			String line;
			Process p = Runtime.getRuntime().exec(args);
			BufferedReader bri = new BufferedReader(new InputStreamReader(
					p.getInputStream()));
			BufferedReader bre = new BufferedReader(new InputStreamReader(
					p.getErrorStream()));
			while ((line = bri.readLine()) != null) {
				System.out.println(line);
			}
			bri.close();
			while ((line = bre.readLine()) != null) {
				System.out.println(line);
			}
			bre.close();
			p.waitFor();
			System.out.println("Done.");
		} catch (Exception err) {
			err.printStackTrace();
		}
	}
}

class CouldNotCreateFolderException extends Exception {

	public CouldNotCreateFolderException() {
	}

	public CouldNotCreateFolderException(String msg) {
		super(msg);
	}
}
